package com.project.controller;

import com.project.bean.BuyBean;
import com.project.bean.BuyCarBean;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.config.UploadUtil;
import com.project.service.IBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("buy")
public class BuyController {
    @Autowired
    private IBuyService service;
    @RequestMapping("add")
    public String add(String method ,ProductBean product, @RequestParam("num")int number, @RequestParam("img") MultipartFile mf, HttpServletRequest request){
        Map<Integer,BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute("");
        String fileName =  UploadUtil.upload(mf, "static/html/img");
        product.setPhoto(fileName);
        service.add(method, product, shopMap, number);
        return "ok";
    }

    @RequestMapping("remove")
    public String remove(int productId,HttpServletRequest request){
        Map<Integer,BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute("");
        service.del(shopMap, productId);
        return "ok";
    }

    @RequestMapping("buy")
    public String buy(int supId,HttpServletRequest request){
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        //得到当前供应商的购物车map
        Map<Integer,BuyCarBean> shopMap = (Map<Integer, BuyCarBean>) request.getSession().getAttribute("");
        BuyBean buy = new BuyBean(supId, user.getName());
        service.sureBuy(shopMap,buy);
        return "ok";
    }





}
